home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Sources / UDialogBehavior.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  8.3 KB  |  307 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UDialogBehavior.cp 
  3. // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UDIALOGBEHAVIOR__
  7. #include "UDialogBehavior.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #if qContainer
  13.     #ifndef __UCONTAINER__
  14.     #include "UContainer.h"
  15.     #endif
  16. #endif
  17.  
  18. #ifndef __UCOREGLOBALS__
  19. #include "UCoreGlobals.h"
  20. #endif
  21.  
  22. #ifndef __UDISPATCHER__
  23. #include "UDispatcher.h"
  24. #endif
  25.  
  26. //    #ifndef __UMACAPPGLOBALS__
  27. //    #include "UMacAppGlobals.h"
  28. //    #endif
  29.  
  30. #ifndef __USTREAM__
  31. #include "UStream.h"
  32. #endif
  33.  
  34. #ifndef __UVIEW__
  35. #include "UView.h"
  36. #endif
  37.  
  38. #if qContainer
  39.     #ifndef __UWINDOW__
  40.     #include "UWindow.h"
  41.     #endif
  42. #endif
  43.  
  44. //========================================================================================
  45. // CLASS TDialogBehavior
  46. //========================================================================================
  47. #undef Inherited
  48. #define Inherited TBehavior
  49.  
  50. #pragma segment MAOpen
  51. MA_DEFINE_CLASS_M1(TDialogBehavior, Inherited);
  52.  
  53. //----------------------------------------------------------------------------------------
  54. // TDialogBehavior constructor
  55. //----------------------------------------------------------------------------------------
  56. #pragma segment MAOpen
  57.  
  58. TDialogBehavior::TDialogBehavior()
  59. {
  60.     fModal = FALSE;
  61.     fDefaultItem = kNoIdentifier;
  62.     fCancelItem  = kNoIdentifier;
  63.     fDismisser   = kNoIdentifier;
  64.     fDismissed   = TRUE;
  65. } // TDialogBehavior::TDialogBehavior
  66.  
  67. //----------------------------------------------------------------------------------------
  68. // TDialogBehavior destructor
  69. //----------------------------------------------------------------------------------------
  70. #pragma segment MADestructorRes
  71.  
  72. TDialogBehavior::~TDialogBehavior()
  73. {
  74. }
  75.  
  76. //----------------------------------------------------------------------------------------
  77. // TDialogBehavior::IDialogBehavior: 
  78. //----------------------------------------------------------------------------------------
  79. #pragma segment MAOpen
  80.  
  81. void TDialogBehavior::IDialogBehavior(Boolean isModal,
  82.                                              IDType  itsDefaultItem,
  83.                                              IDType  itsCancelItem)    
  84. {
  85.     this->IBehavior(kDialogBehavior);
  86.  
  87.     fModal = isModal;
  88.     fDefaultItem = itsDefaultItem;
  89.     fCancelItem = itsCancelItem;
  90. } // TDialogBehavior::IDialogBehavior 
  91.  
  92. //----------------------------------------------------------------------------------------
  93. // TDialogBehavior::ReadFrom: 
  94. //----------------------------------------------------------------------------------------
  95. #pragma segment MAReadResource
  96.  
  97. void TDialogBehavior::ReadFrom(TStream* aStream)                // override 
  98. {
  99.     Inherited::ReadFrom(aStream);
  100.  
  101.     fModal = aStream->ReadBoolean();
  102.      fDefaultItem = aStream->ReadIDType();
  103.     fCancelItem = aStream->ReadIDType();
  104. } // TDialogBehavior::ReadFrom 
  105.  
  106. //----------------------------------------------------------------------------------------
  107. // TDialogBehavior::WriteTo: 
  108. //----------------------------------------------------------------------------------------
  109. #pragma segment MAWriteResource
  110.  
  111. void TDialogBehavior::WriteTo(TStream* aStream)                // override 
  112. {
  113.     Inherited::WriteTo(aStream);
  114.  
  115.     aStream->WriteBoolean(fModal);
  116.     aStream->WriteIDType(fDefaultItem);
  117.     aStream->WriteIDType(fCancelItem);
  118. } // TDialogBehavior::WriteTo 
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // TDialogBehavior::Dismiss: 
  122. //----------------------------------------------------------------------------------------
  123. #pragma segment MAWindowRes
  124.  
  125. void TDialogBehavior::Dismiss(IDType dismisser, Boolean validate)
  126. {
  127.     TView* owner = ( TView* )fOwner;
  128.  
  129.     if (owner)
  130.     {
  131.         if (validate)
  132.         {
  133.             if (owner->IsHierarchyValid())
  134.             {
  135.                 fDismisser = dismisser;
  136.                 fDismissed = TRUE;
  137.             }
  138.         }
  139.         else
  140.         {
  141.             fDismisser = dismisser;
  142.             fDismissed = TRUE;            
  143.         }
  144.         
  145.     }
  146.     
  147. } // TDialogBehavior::Dismiss 
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // TDialogBehavior::DoEvent: 
  151. //----------------------------------------------------------------------------------------
  152. #pragma segment MAWindowRes
  153.  
  154. void TDialogBehavior::DoEvent(EventNumber eventNumber,
  155.                                  TEventHandler* source,
  156.                                  TEvent* event)// override 
  157. {
  158.     if (eventNumber == mDismiss /*dismiss*/)
  159.     {
  160.         this->Dismiss(source->fIdentifier,source->fIdentifier != fCancelItem);
  161.     }
  162.     else
  163.         Inherited::DoEvent(eventNumber, source, event);
  164.     
  165. } // TDialogBehavior::DoEvent 
  166.  
  167. //----------------------------------------------------------------------------------------
  168. // TDialogBehavior::DoKeyEvent: 
  169. //----------------------------------------------------------------------------------------
  170. #pragma segment MAWindowRes
  171.  
  172. void TDialogBehavior::DoKeyEvent(TToolboxEvent* event)// override 
  173. {
  174.     // Maps the Enter and Return keys to the default item (view)
  175.     // Maps the Escape key to the cancel item (view)
  176.     
  177.     TView* owner = (TView*)fOwner;
  178.     Boolean wasHandled = FALSE;
  179.  
  180.     if (owner && owner->IsEnabled())
  181.     {
  182.         if (event->fText == chEscape && event->fKeyCode != kClearVirtualCode) // esc double for two different keys! 
  183.         {
  184.             if (fCancelItem != kNoIdentifier)
  185.             {
  186.                 wasHandled = TRUE;
  187.                 TView * cancelView = owner->FindSubView(fCancelItem);
  188.                 if (cancelView)
  189.                 {
  190.                     if (cancelView->IsEnabled())
  191.                         cancelView->HandleEvent(cancelView->GetEventNumber(), owner, NULL);
  192.                 }
  193.                 else
  194.                     owner->HandleEvent(mCancelKey, owner, NULL);
  195.             }
  196.         }
  197.         else if(event->fText == chEnter || event->fText == chReturn)
  198.         {
  199.             if (fDefaultItem != kNoIdentifier)
  200.             {
  201.                 wasHandled = TRUE;
  202.                 TView * defaultView = owner->FindSubView(fDefaultItem);
  203.                 if (defaultView)
  204.                 {
  205.                     if (defaultView->IsEnabled())
  206.                         defaultView->HandleEvent(defaultView->GetEventNumber(), owner, NULL);
  207.                 }
  208.                 else
  209.                     owner->HandleEvent(mDefaultKey, owner, NULL);
  210.             }
  211.         }
  212.     }
  213.  
  214.     if (!wasHandled)
  215.         Inherited::DoKeyEvent(event);
  216. } // TDialogBehavior::DoKeyEvent 
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // TDialogBehavior::DoCommandKeyEvent: 
  220. //----------------------------------------------------------------------------------------
  221. #pragma segment MAWindowRes
  222.  
  223. void TDialogBehavior::DoCommandKeyEvent(TToolboxEvent* event)// override 
  224.  
  225. {
  226.     // Maps Command-Period to the cancel item (view)
  227.     TView* owner = (TView*)fOwner;
  228.  
  229.     if (owner && owner->IsEnabled() && event->fText == '.' && fCancelItem != kNoIdentifier)
  230.     {
  231.         TView* cancelView = owner->FindSubView(fCancelItem);
  232.         if (cancelView)
  233.         {
  234.             if (cancelView->IsEnabled())
  235.                 cancelView->HandleEvent(cancelView->GetEventNumber(), owner, NULL);
  236.         }
  237.         else
  238.             owner->HandleEvent(mCancelKey, owner, NULL);
  239.     }
  240.     else
  241.         Inherited::DoCommandKeyEvent(event);
  242. } // TDialogBehavior::DoCommandKeyEvent 
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // TDialogBehavior::PoseModally: 
  246. //----------------------------------------------------------------------------------------
  247. #pragma segment MAWindowNonRes
  248.  
  249. void TDialogBehavior::PoseModally()
  250. {
  251. #if qContainer
  252.     WindowPtr dialogWindow = NULL;
  253.     if (gContainerLib)
  254.     {
  255.         TView* owner = (TView*) fOwner;
  256.         if (owner)
  257.         {
  258.             TWindow* window = owner->GetWindow();
  259.             if (window)
  260.                 dialogWindow = window->fWMgrWindow;
  261.         }
  262.     }
  263.     
  264.     CModalFocus dialogFocus(dialogWindow);
  265.     if (dialogFocus.Request())
  266.     {
  267. #endif
  268.         fDismissed = FALSE;
  269.         fDismisser = kNoIdentifier;
  270.         MAVolatile(short, oldEventMask);
  271.         oldEventMask = gDispatcher->GetMainEventMask();
  272.         gDispatcher->SetMainEventMask(oldEventMask & ~highLevelEventMask);
  273.     
  274.         FailInfo fi;
  275.         Try(fi)
  276.         {
  277.             while (!fDismissed)
  278.                 gDispatcher->PollEvent(kAllowApplicationToSleep);
  279.     
  280.             gDispatcher->SetMainEventMask(oldEventMask);
  281.             fi.Success();
  282.         }
  283.         else                                // Recover
  284.         {
  285.             gDispatcher->SetMainEventMask(oldEventMask);
  286.             fi.ReSignal();
  287.         }
  288. #if qContainer
  289.     }
  290. #endif
  291. } // TDialogBehavior::PoseModally 
  292.  
  293. //----------------------------------------------------------------------------------------
  294. // TDialogBehavior::GetStandardSignature:
  295. //----------------------------------------------------------------------------------------
  296. #pragma segment MAWriteResource
  297.  
  298. IDType TDialogBehavior::GetStandardSignature()    // override 
  299. {
  300.     return kStdDialogBehavior;
  301. } // TDialogBehavior::GetStandardSignature
  302.  
  303. //----------------------------------------------------------------------------------------
  304. // End of UDialogBehavior.cp
  305.  
  306. #pragma segment Inline
  307.